//
//  LHTabBarController.swift
//  LH-Swift-WeiBo
//
//  Created by liuhao on 16/9/22.
//  Copyright © 2016年 liuhao. All rights reserved.
//

import UIKit

class LHTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //将tabBar替换为自定义布局的tabBar
        
        let hmTabbar = LHMainTabBarView()
        
        
        //实现闭包  可以传值
        hmTabbar.composeClosure = {[weak self] in
            
            print(">>>>>>>>>>⚔⚔⚔⚔我在这里⚔⚔⚔⚔<<<<<<<<<<<<")
            print(self, "被点击")
            print(">>>>>>>>>>⚔⚔⚔⚔我在这里⚔⚔⚔⚔<<<<<<<<<<<<")
        
        
        
        }
        
        self.setValue(hmTabbar, forKey: "tabBar")
        
        
        //添加控制器
        self.addViewControllers()
        
        
    }
    
    //自定义方法添加控制器
    private func addViewControllers () {
        
        self.addChildViewController(vc: LHHomeTableController(), title: "首页", imageName: "tabbar_home")
        
        self.addChildViewController(vc: LHMessageTableController(), title: "消息", imageName: "tabbar_message_center")
        
        self.addChildViewController(vc: LHDiscoverTableController(), title: "发现", imageName: "tabbar_discover")
        
        self.addChildViewController(vc: LHHomeTableController(), title: "我", imageName: "tabbar_profile")
        
    }
    
    
    //重载 方法 设置参数 设置属性
    private func addChildViewController(vc : UIViewController,title : String,imageName : String) {
    
        vc.navigationItem.title = title
        
        vc.tabBarItem.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        
        //设置选中的图片 并取消系统的渲染
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        
        //设置文字颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
        
        vc.tabBarItem.badgeColor = UIColor.yellow
        
        vc.tabBarItem.badgeValue = nil
        
        let nav = LHBaseNavController(rootViewController: vc)
        
        // addChildViewController: 如果在自定义tabBar 将tabBar上的tabBarButton隐藏 自己添加自定义按钮 并且还使用该方法 就会楚翔 自定义按钮无法点击, 使用 setViewControllers: animated: 就可以解决该问题
        self.addChildViewController(nav)
        
        
        
        
    
        
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
